package com.antler.luajavalibrary.luaext.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.antler.luajavalibrary.application.MyApplication;
import com.antler.luajavalibrary.utils.FileUtils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;

public class CommonUtils {
    private static int sStatusBarHeight = -1;

    private CommonUtils() {
        throw new AssertionError();
    }

    public static int getStatusBarHeight() {
        Context context = MyApplication.getContext();

        if (sStatusBarHeight == -1) {
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object o = c.newInstance();
                Field field = c.getField("status_bar_height");
                int x = field.getInt(o);
                sStatusBarHeight = context.getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sStatusBarHeight;
    }

    public static int getStatusBatHeight2() {
        Context context = MyApplication.getContext();

        if (sStatusBarHeight == -1) {
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                sStatusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
                float scale = context.getResources().getDisplayMetrics().density;
                sStatusBarHeight = (int) (sStatusBarHeight * scale + 0.5f);
            }
        }
        return sStatusBarHeight;
    }

    public static byte[] assetsLoader(String filename) {
        Context context = MyApplication.getContext();

        InputStream is = null;
        try {
            AssetManager am = context.getAssets();
            is = am.open(filename);

            return FileUtils.readAll(is);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
